package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.InfoDao;
import com.bestv.search.common.dao.SearchKeywordHistoryDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.Info;
import com.bestv.search.common.model.InfoHeaderItem;
import com.bestv.search.common.model.TopNCache;
import com.bestv.search.common.service.InfoManager;

import java.text.ParseException;
import java.util.*;

//$Id: InfoManagerImpl.java 29 2012-03-13 03:37:49Z wbwang $
public class InfoManagerImpl implements InfoManager {
    private InfoDao infoDao;
    private SearchKeywordHistoryDao searchKeywordHistoryDao;

    public InfoDao getInfoDao() {
        return infoDao;
    }

    public void setInfoDao(InfoDao infoDao) {
        this.infoDao = infoDao;
    }

    public SearchKeywordHistoryDao getSearchKeywordHistoryDao() {
        return searchKeywordHistoryDao;
    }

    public void setSearchKeywordHistoryDao(SearchKeywordHistoryDao searchKeywordHistoryDao) {
        this.searchKeywordHistoryDao = searchKeywordHistoryDao;
    }

    @Override
    public Info get(DataSourceKey key, long id) {
        return infoDao.get(id);
    }

    @Override
    public List<Info> getInfoByCode(DataSourceKey key, String code) {
        return infoDao.getInfoByCode(code);
    }

    @Override
    public List<Info> getInfoByCode(DataSourceKey key, List<String> codes) {
        return infoDao.getInfoByCode(codes);
    }

    @Override
    public List<Info> getInfoListByIds(DataSourceKey key, List<Long> ids) {
        List<Info> list = new ArrayList<Info>();
        for (long id : ids) {
            Info info = infoDao.get(id);
            list.add(info);
        }
        return list;
    }

    @Override
    public boolean saveOffLineInfoByCode(DataSourceKey key, List<String> codes) {
        List<Info> infos = infoDao.getInfoByCode(codes);
        if (infos != null && infos.size() > 0) {
            for (Info info : infos) {
                info.setUpdateFlag(2);
                info.setUpdateTimeForIndex(new Date());
                infoDao.saveOrUpdate(info);
            }

            return true;
        }
        return false;
    }

    @Override
    public List<Info> getInfoLikeName(DataSourceKey key, String name, int start, int limit) {
        return infoDao.getInfoLikeName(name, start, limit);
    }

    @Override
    public int getInfoCountLikeName(DataSourceKey key, String name) {
        return infoDao.getInfoCountLikeName(name);
    }

    @Override
    public List<Info> getInfoByName(DataSourceKey key, String[] names, String p_type) {
        return infoDao.getInfoByNames(names, p_type);
    }

    @Override
    public List<Info> getInfoByName(DataSourceKey key, String[] names) {
        return infoDao.getInfoByNames(names);
    }

    @Override
    public String getNetInfoIdByMediaCode(DataSourceKey key, String mediaCode) {
        return infoDao.getNetInfoIdByMediaCode(mediaCode);
    }

    @Override
    public List<Object[]> getComparedInfo(DataSourceKey key, String mediaCode, String p_type) {
        return infoDao.getComparedInfo(mediaCode, p_type);
    }

    @Override
    public List<Info> searchInfoByNames(DataSourceKey key, String[] names, int firstResult, int maxResult) {
        return infoDao.searchInfoByNames(names, firstResult, maxResult);
    }

    @Override
    public int getRowCount(DataSourceKey key, String[] names) {
        return infoDao.getRowCount(names);
    }

    @Override
    public Set<Info> getInfoSetByIds(DataSourceKey key, Object ids) {
        return infoDao.getInfoSetByIds(ids);
    }

    @Override
    public Set<Info> getInfoSetByCodes(DataSourceKey key, Object codes) {
        return infoDao.getInfoSetByCodes(codes);
    }

    @Override
    public List<String> findPTypes(DataSourceKey key) {
        return infoDao.findPTypes();
    }

    @Override
    public Map<String, Integer[]> findProAndKeyNumInPTypes(DataSourceKey key) {
        return infoDao.findProAndKeyNumInPTypes();
    }

    @Override
    public Map<String, Integer[]> findProAndTagNumInPTypes(DataSourceKey key) {
        return infoDao.findProAndTagNumInPTypes();
    }

    @Override
    public String[][] findEachKeyAndNumInPType(DataSourceKey key, String ptype, int start, int limit) {
        return infoDao.findEachKeyAndNumInPType(ptype, start, limit);
    }

    @Override
    public int getDistinctCountOfKeysInPtype(DataSourceKey key, String ptype) {
        return infoDao.getDistinctCountOfKeysInPtype(ptype);
    }

    @Override
    public int getDistinctCountOfTagsInPtype(DataSourceKey key, String ptype) {
        return infoDao.getDistinctCountOfTagsInPtype(ptype);
    }

    @Override
    public List<TopNCache> getInfosTopNCache(DataSourceKey key, String category, String type) {
        return infoDao.getInfosTopNCache(category, type);
    }

    @Override
    public String[][] findEachTagAndNumInPtype(DataSourceKey key, String ptype, int start, int limit) {
        return infoDao.findEachTagAndNumInPtype(ptype, start, limit);
    }

    @Override
    public List<Integer> findCrossKeyword(DataSourceKey key) {
        return infoDao.findCrossKeyword();
    }

    @Override
    public List<String> findCrossKeywordDetail(DataSourceKey key, String ptype1, String ptype2, int start, int limit) {
        return infoDao.findCrossKeywordDetail(ptype1, ptype2, start, limit);
    }

    @Override
    public Set<Info> getInfoSetByIds(DataSourceKey key, String ids, Map<String, String> otherConditions)
            throws ParseException {
        if (otherConditions.size() == 0)
            return getInfoSetByIds(key, ids);
        return infoDao.getInfoSetByIds(ids, otherConditions);
    }

    @Override
    public List<Info> getInfosByType(DataSourceKey key, int stat, int lmt, String infoType) {
        return infoDao.getInfosByType(stat, lmt, infoType);
    }

    @Override
    public int getCountByType(DataSourceKey key, String infoType) {
        return infoDao.getCountByType(infoType);
    }

    @Override
    public void delete(DataSourceKey key, Long id) {
        infoDao.delete(id);
    }

    @Override
    public void saveInfoList(DataSourceKey key, List<Info> list) {
        infoDao.saveInfoList(list);
    }

    @Override
    public void saveUpdateInfoList(DataSourceKey key, List<Info> list) {
        infoDao.saveUpdateInfoList(list);
    }

    @Override
    public void saveUpdateInfo(DataSourceKey key, Info info) {
        infoDao.saveUpdateInfo(info);
    }

    @Override
    public List<Info> getOverdueSchedule(DataSourceKey key, int validPeiod) {
        return infoDao.getOverdueSchedule(validPeiod);
    }

    @Override
    public int fingCrossKeyCount(DataSourceKey key, String ptype1, String ptype2) {
        return infoDao.findCrossKeyCount(ptype1, ptype2);
    }

    @Override
    public List<String> getInfoCodesByNameGenreTag(DataSourceKey key, String word) {
        return infoDao.getInfoCodesByNameGenreTag(word);
    }

    @Override
    public int getOnLineInfosCount(DataSourceKey key) {
        return infoDao.getOnLineInfosCount();
    }

    @Override
    public List<Info> getOnLineInfos(DataSourceKey key, int start, int limit) {
        return infoDao.getOnLineInfos(start, limit);
    }

    @Override
    public List<Info> getInfosByUpdateTimeForIndex(DataSourceKey key, Date updateIndexTimeForIndex) {
        return infoDao.getInfosByUpdateTimeForIndex(updateIndexTimeForIndex);
    }

    @Override
    public Map<String, Integer> getMaxHitCountByTags(DataSourceKey key, String p_type) {
        return infoDao.getMaxHitCountByTags(p_type);
    }

    @Override
    public void saveOrUpdateInfoByInfoHeaderItems(DataSourceKey key, List<InfoHeaderItem> items) {
        if (items != null) {
            for (InfoHeaderItem item : items) {
                Info info = infoDao.get(item.getInfo_id());
                if (info != null) {
                    info.setContent_flag(item.getContentFlag());
                    info.setContent_version(item.getContentVersion());
                    info.setUpdateFlag(1);
                    info.setUpdateTimeForIndex(new Date());
                    infoDao.update(info);
                }
            }
        }
    }

    @Override
    public void saveInfoUpFlag(DataSourceKey key, Info info) {
        if (info != null) {
            // Handle info
            Info infoInDB = infoDao.get(info.getId());
            if (infoInDB != null) {
                infoInDB.setUp_flag(info.getUp_flag());
                infoInDB.setUpdateTimeForIndex(new Date());
                infoDao.update(infoInDB);
            }
        }
    }
}
